//Ther Year of Code Jam
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;

struct Edge {int to,  cap, rev; };

const int max_v = 2510;
const int INF = 1 << 30;

vector<Edge> G[max_v];
int level[max_v], iter[max_v];

void add_edge(int from, int to, int cap)
{
	G[from].push_back((Edge){to, cap, (int)G[to].size()});
	G[to].push_back((Edge){from, 0, (int)G[from].size() - 1});
}

void bfs(int s)
{
	memset(level, -1, sizeof(level));
	queue<int> que;
	level[s] = 0;
	que.push(s);
	while (!que.empty()) {
		int v = que.front(); que.pop();
		for (int i = 0; i < (int)G[v].size(); ++i) {
			Edge &e = G[v][i];
			if (0 < e.cap && level[e.to] < 0) {
				level[e.to] = level[v] + 1;
				que.push(e.to);
			}
		}
	}
}

int dfs(int v, int t, int f)
{
	if (v == t) return f;
	for (int &i = iter[v]; i < (int)G[v].size(); ++i) {
		Edge &e = G[v][i];
		if (0 < e.cap && level[v] < level[e.to]) {
			int d = dfs(e.to, t, min(f, e.cap));
			if (d > 0) {
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}

int max_flow(int s, int t)
{
	int flow = 0;
	for (;;) {
		bfs(s);
		if (level[t] < 0) return flow;
		memset(iter, 0, sizeof(iter));
		int f;
		while ((f = dfs(s, t, INF)) > 0) flow += f;
	}
}

int main()
{
	const int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
	int t;
	cin >> t;
	for (int ti = 1; ti <= t; ++ti){
		int n, m, ans = 0;
		cin >> n >> m;
		for (int i = 0; i < n * m + 2; ++i) G[i].clear();

		char da[n][m];
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < m; ++j) cin >> da[i][j];

		int s = n * m, t = s + 1;
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < m; ++j)
			if (!((i + j) & 1)) {
				if (da[i][j] == '#') {
					ans += 4;
					add_edge(s, i * m + j, INF);
				} else
				if (da[i][j] == '.') add_edge(i * m + j, t, INF);
				else {
					ans += 4;
					add_edge(s, i * m + j, 4);
				}
				for (int k = 0; k < 4; ++k) {
					int i2 = i + dx[k], j2 = j + dy[k];
					if (0 <= i2 && i2 < n && 0 <= j2 && j2 < m)
						add_edge(i * m + j, i2 * m + j2, 2);
				}
			} else {
				if (da[i][j] == '#') {
					ans += 4;
					add_edge(i * m + j, t, INF);
				} else
				if (da[i][j] == '.') add_edge(s, i * m + j, INF);
				else {
					ans += 4;
					add_edge(i * m + j, t, 4);
				}
			}
		ans -= max_flow(s, t);
		cout << "Case #" << ti << ": " << ans << endl;
	}
	return 0;
}
